home *** CD-ROM | disk | FTP | other *** search
/ Linux Cubed Series 7: Sunsite / Linux Cubed Series 7 - Sunsite Vol 1.iso / system / shells / rc-1.000 / rc-1 / rc-1.5-linux / fn.c < prev    next >
Encoding:
C/C++ Source or Header  |  1994-03-07  |  6.3 KB  |  258 lines

  1. /*
  2.    fn.c: functions for adding and deleting functions from the symbol table.
  3.    Support for signal handlers is also found here.
  4. */
  5.  
  6. #include <signal.h>
  7. #include <errno.h>
  8. #include "rc.h"
  9. #include "sigmsgs.h"
  10.  
  11. static void fn_handler(int), dud_handler(int);
  12.  
  13. static bool runexit = FALSE;
  14. static Node *handlers[NUMOFSIGNALS], null;
  15. static void (*def_sigint)(int) = SIG_DFL;
  16. static void (*def_sigquit)(int) = SIG_DFL;
  17. static void (*def_sigterm)(int) = SIG_DFL;
  18.  
  19. /*
  20.    Set signals to default values for rc. This means that interactive
  21.    shells ignore SIGTERM, etc.
  22. */
  23.  
  24. extern void inithandler() {
  25.     int i;
  26.     null.type = nBody;
  27.     null.u[0].p = null.u[1].p = NULL;
  28.     for (i = 1; i < NUMOFSIGNALS; i++)
  29. #ifdef NOSIGCLD
  30.         if (i != SIGCLD)
  31. #endif
  32.         if (sighandlers[i] == SIG_IGN)
  33.             fnassign(signals[i].name, NULL); /* ignore incoming ignored signals */
  34.     if (interactive || sighandlers[SIGINT] != SIG_IGN) {
  35.         def_sigint = sigint;
  36.         fnrm("sigint"); /* installs SIGINT catcher if not inherited ignored */
  37.     }
  38.     if (!dashdee) {
  39.         if (interactive || sighandlers[SIGQUIT] != SIG_IGN) {
  40.             def_sigquit = dud_handler;
  41.             fnrm("sigquit"); /* "ignores" SIGQUIT unless inherited ignored */
  42.         }
  43.         if (interactive) {
  44.             def_sigterm = dud_handler;
  45.             fnrm("sigterm"); /* ditto for SIGTERM */
  46.         }
  47.     }
  48. }
  49.  
  50. /* only run this in a child process! resets signals to their default values */
  51.  
  52. extern void setsigdefaults(bool sysvbackground) {
  53.     int i;
  54.     /*
  55.        General housekeeping: setsigdefaults happens after fork(),
  56.        so it's a convenient place to clean up open file descriptors.
  57.        (history file, scripts, etc.)
  58.     */
  59.     closefds();
  60.     /*
  61.        Restore signals to SIG_DFL, paying close attention to
  62.        a few quirks: SIGINT, SIGQUIT and are treated specially
  63.        depending on whether we are doing v7-style backgrounding
  64.        or not; the default action for SIGINT, SIGQUIT and SIGTERM
  65.        must be set to the appropriate action; finally, care must
  66.        be taken not to set to SIG_DFL any signals which are being
  67.        ignored.
  68.     */
  69.     for (i = 1; i < NUMOFSIGNALS; i++)
  70.         if (sighandlers[i] != SIG_IGN) {
  71.             handlers[i] = NULL;
  72.             switch (i) {
  73.             case SIGINT:
  74.                 if (sysvbackground) {
  75.                     def_sigint = SIG_IGN;
  76.                     fnassign("sigint", NULL); /* ignore */
  77.                 } else {
  78.                     def_sigint = SIG_DFL;
  79.                     goto sigcommon;
  80.                 }
  81.                 break;
  82.             case SIGQUIT:
  83.                 if (sysvbackground) {
  84.                     def_sigquit = SIG_IGN;
  85.                     fnassign("sigquit", NULL); /* ignore */
  86.                 } else {
  87.                     def_sigquit = SIG_DFL;
  88.                     goto sigcommon;
  89.                 }
  90.                 break;
  91.             case SIGTERM:
  92.                 def_sigterm = SIG_DFL;
  93.                 /* FALLTHROUGH */
  94.             sigcommon:
  95.             default:
  96.                 if (sighandlers[i] != SIG_DFL) {
  97.                     rc_signal(i, SIG_DFL);
  98.                     delete_fn(signals[i].name);
  99.                 }
  100.             }
  101.         }
  102.     delete_fn("sigexit");
  103.     runexit = FALSE; /* No sigexit on subshells */
  104. }
  105.  
  106. /* rc's exit. if runexit is set, run the sigexit function. */
  107.  
  108. extern void rc_exit(int stat) {
  109.     if (runexit) {
  110.         char *sig[2];
  111.         sig[0] = "sigexit";
  112.         sig[1] = NULL;
  113.         runexit = FALSE;
  114.         funcall(sig);
  115.         stat = getstatus();
  116.     }
  117.     exit(stat);
  118. }
  119.  
  120. /* The signal handler for all functions. calls walk() */
  121.  
  122. static void fn_handler(int s) {
  123.     char *sig[2];
  124.     int olderrno;
  125.     if (s < 1 || s >= NUMOFSIGNALS)
  126.         panic("unknown signal");
  127.     olderrno = errno;
  128.     sig[0] = signals[s].name;
  129.     sig[1] = NULL;
  130.     funcall(sig);
  131.     errno = olderrno;
  132. }
  133.  
  134. /* A dud signal handler for SIGQUIT and SIGTERM */
  135.  
  136. static void dud_handler(int s) {
  137. }
  138.  
  139. /*
  140.    Assign a function in Node form. Check to see if the function is also
  141.    a signal, and set the signal vectors appropriately.
  142. */
  143.  
  144. extern void fnassign(char *name, Node *def) {
  145.     Node *newdef = treecpy(def == NULL ? &null : def, ealloc); /* important to do the treecopy first */
  146.     Function *new = get_fn_place(name);
  147.     int i;
  148.     new->def = newdef;
  149.     new->extdef = NULL;
  150.     if (strncmp(name, "sig", conststrlen("sig")) == 0) { /* slight optimization */
  151. #ifdef NOSIGCLD /* System V machines treat SIGCLD very specially */
  152.         if (streq(name, "sigcld"))
  153.             rc_error("can't trap SIGCLD");
  154. #endif
  155.         if (streq(name, "sigexit"))
  156.             runexit = TRUE;
  157.         for (i = 1; i < NUMOFSIGNALS; i++) /* zero is a bogus signal */
  158.             if (streq(signals[i].name, name)) {
  159.                 handlers[i] = newdef;
  160.                 if (def == NULL)
  161.                     rc_signal(i, SIG_IGN);
  162.                 else
  163.                     rc_signal(i, fn_handler);
  164.                 break;
  165.             }
  166.     }
  167. }
  168.  
  169. /* Assign a function from the environment. Store just the external representation */
  170.  
  171. extern void fnassign_string(char *extdef) {
  172.     char *name = get_name(extdef+3); /* +3 to skip over "fn_" */
  173.     Function *new;
  174.     if (name == NULL)
  175.         return;
  176.     new = get_fn_place(name);
  177.     new->def = NULL;
  178.     new->extdef = ecpy(extdef);
  179. }
  180.  
  181. /* Return a function in Node form, evaluating an entry from the environment if necessary */
  182.  
  183. extern Node *fnlookup(char *name) {
  184.     Function *look = lookup_fn(name);
  185.     Node *ret;
  186.     if (look == NULL)
  187.         return NULL; /* not found */
  188.     if (look->def != NULL)
  189.         return look->def;
  190.     if (look->extdef == NULL) /* function was set to null, e.g., fn foo {} */
  191.         return &null;
  192.     ret = parse_fn(name, look->extdef);
  193.     if (ret == NULL) {
  194.         efree(look->extdef);
  195.         look->extdef = NULL;
  196.         return &null;
  197.     } else {
  198.         return look->def = treecpy(ret, ealloc); /* Need to take it out of nalloc space */
  199.     }
  200. }
  201.  
  202. /* Return a function in string form (used by makeenv) */
  203.  
  204. extern char *fnlookup_string(char *name) {
  205.     Function *look = lookup_fn(name);
  206.  
  207.     if (look == NULL)
  208.         return NULL;
  209.     if (look->extdef != NULL)
  210.         return look->extdef;
  211.     return look->extdef = mprint("fn_%F={%T}", name, look->def);
  212. }
  213.  
  214. /*
  215.    Remove a function from the symbol table. If it also defines a signal
  216.    handler, restore the signal handler to its default value.
  217. */
  218.  
  219. extern void fnrm(char *name) {
  220.     int i;
  221.     for (i = 1; i < NUMOFSIGNALS; i++)
  222.         if (streq(signals[i].name, name)) {
  223.             handlers[i] = NULL;
  224.             switch (i) {
  225.             case SIGINT:
  226.                 rc_signal(i, def_sigint);
  227.                 break;
  228.             case SIGQUIT:
  229.                 rc_signal(i, def_sigquit);
  230.                 break;
  231.             case SIGTERM:
  232.                 rc_signal(i, def_sigterm);
  233.                 break;
  234.             default:
  235.                 rc_signal(i, SIG_DFL);
  236.             }
  237.         }
  238.     if (streq(name, "sigexit"))
  239.         runexit = FALSE;
  240.     delete_fn(name);
  241. }
  242.  
  243. extern void whatare_all_signals() {
  244.     int i;
  245.     for (i = 1; i < NUMOFSIGNALS; i++)
  246.         if (*signals[i].name != '\0')
  247.             if (sighandlers[i] == SIG_IGN)
  248.                 fprint(1, "fn %s {}\n", signals[i].name);
  249.             else if (sighandlers[i] == fn_handler)
  250.                 fprint(1, "fn %S {%T}\n", signals[i].name, handlers[i]);
  251.             else
  252.                 fprint(1, "fn %s\n", signals[i].name);
  253. }
  254.  
  255. extern void prettyprint_fn(int fd, char *name, Node *n) {
  256.     fprint(fd, "fn %S {%T}\n", name, n);
  257. }
  258.